﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MultipleQueue.Entities;
using MultipleQueue.Distributions;
using MultipleQueue.Simulation;

namespace MultipleQueue
{
    public partial class MultipleServerSimulation : Form
    {
        private int numberOfServers;
        private Server[] servers;
        private string stoppingConditionType;
        private int stoppingConditionValue;
        //private IDistribution serviceDistributionType;
        private IDistribution interarrivalDistributionType;
        private ServerSelection serverSelection;


        SimulationManager simManager;

        public MultipleServerSimulation()
        {
            InitializeComponent();
            this.dataGridViewservers.AllowUserToDeleteRows = false;
            this.dataGridViewservers.AllowUserToAddRows = false;
            simManager = new SimulationManager();
            this.WindowState = FormWindowState.Maximized;
        }

        private void priorityRadioBtn_CheckedChanged(object sender, EventArgs e)
        {
            if (this.numServersTxt.Text != string.Empty)
            {
                this.dataGridViewservers.Enabled = true;
                DataTable servers = new DataTable();
                servers.Columns.Add("Servers").ReadOnly = true;
                servers.Columns.Add("Priority");

                for (int i = 1; i <= int.Parse(this.numServersTxt.Text); i++)
                {
                    servers.Rows.Add(i);
                }

                this.dataGridViewservers.DataSource = servers;
                serverSelection=new ServerSelection(ServerSelection.ServerSelectionTypes.HighestPriority);
            }
            else
            {
                MessageBox.Show("Please specify the number of servers.");
            }
        }



        private void rdBtnDiscrete_CheckedChanged(object sender, EventArgs e)
        {
            this.dataGridInterarrivalTime.Enabled = true;
            this.txtExpMean.Enabled = false;
            this.txtMax.Enabled = false;
            this.txtMin.Enabled = false;
            DataTable interarrivalTime = new DataTable();
            interarrivalTime.Columns.Add("Interarrival Time values");
            interarrivalTime.Columns.Add("Probability");

            this.dataGridInterarrivalTime.DataSource = interarrivalTime;
        }

        private void BtnStartSimulation_Click(object sender, EventArgs e)
        {
            if (this.numServersTxt.Text == string.Empty)
            {
                MessageBox.Show("Please specify the number of servers.");
            }
            else
                if (this.txtStoppingCon.Text == string.Empty)
                {
                    MessageBox.Show("Please specify the stopping condition.");
                }
                else
                    if (this.rdBtnExp.Checked && this.txtExpMean.Text == string.Empty)
                    {
                        MessageBox.Show("Please specify a mean value for the exponential distribution of the interarrival time.");
                    }
                    else
                    {
                        if (rdBtnNumberCust.Checked)
                        {
                            this.stoppingConditionType = "number";
                        }
                        else
                        {
                            this.stoppingConditionType = "time";
                        }

                        this.stoppingConditionValue = int.Parse(this.txtStoppingCon.Text);
                        
                        if (this.rdBtnDiscrete.Checked)
                        {
                            if (this.dataGridInterarrivalTime.RowCount < 1)
                            {
                                MessageBox.Show("Please specify discrete values and their probabilities for interarrival time.");
                            }
                            else
                            {
                                List<float> interarrivalValues = new List<float>();
                                List<float> interarrivalProbabilities = new List<float>();
                                for (int i = 0; i < this.dataGridInterarrivalTime.RowCount -1 ; i++)
                                {
                                    if (this.dataGridInterarrivalTime.Rows[i].Cells[0].Value.ToString() == string.Empty || this.dataGridInterarrivalTime.Rows[i].Cells[1].ToString() == string.Empty)
                                    {
                                        MessageBox.Show("Please specify discrete values and their probabilities for interarrival time.");
                                        break;
                                    }
                                    else
                                    {
                                        interarrivalValues.Add(float.Parse(this.dataGridInterarrivalTime.Rows[i].Cells[0].Value.ToString()));
                                        interarrivalProbabilities.Add(float.Parse(this.dataGridInterarrivalTime.Rows[i].Cells[1].Value.ToString()));
                                    }
                                }
                                DiscreteDistribution discrete = new DiscreteDistribution(interarrivalValues, interarrivalProbabilities);
                                this.interarrivalDistributionType = discrete;
                            }
                        }
                        else
                            if (this.rdBtnUniform.Checked)
                            {
                                if (this.txtMin.Text == string.Empty || this.txtMax.Text == string.Empty)
                                {
                                    MessageBox.Show("Please specify a range for the uniform distribution of the interarrival time.");
                                }
                                else
                                {
                                    UniformDistribution uniform = new UniformDistribution(float.Parse(this.txtMin.Text), float.Parse(txtMax.Text));
                                    this.interarrivalDistributionType = uniform;
                                }
                            }
                            else
                            {
                                if (this.txtExpMean.Text == string.Empty)
                                {
                                    MessageBox.Show("Please specify a mean value for the exponential distribution of the interarrival time.");
                                }
                                else
                                {
                                    ExponentialDistribution exponential = new ExponentialDistribution(float.Parse(this.txtExpMean.Text));
                                    this.interarrivalDistributionType = exponential;
                                }
                            }

                        simManager.StartSimulation(numberOfServers, servers, serverSelection, stoppingConditionType, stoppingConditionValue, interarrivalDistributionType);
                        this.btnPerformance.Enabled = true;
                    }
        }

        public void SelectionCondition()
        {
            Server tempSever;
            this.numberOfServers = int.Parse(numServersTxt.Text);
            servers = new Server[this.numberOfServers];

            if (this.priorityRadioBtn.Checked)
            {
                for (int i = 0; i < servers.Length; i++)
                {
                    if (this.dataGridViewservers.Rows[i].Cells[1].Value.ToString() == string.Empty)
                    {
                        MessageBox.Show("Please specify priorities for all servers.");
                        break;
                    }
                    else
                    {
                        tempSever = new Server(this.serviceTimeDistributions[i], int.Parse(this.dataGridViewservers.Rows[i].Cells[1].Value.ToString()));
                        servers[i] = tempSever;
                    }
                }
            }
            else if (this.randomRadioBtn.Checked)
            {
                for (int i = 0; i < servers.Length; i++)
                {
                    tempSever = new Server(this.serviceTimeDistributions[i], null);
                    servers[i] = tempSever;
                }
            }
            else
            {
                for (int i = 0; i < servers.Length; i++)
                {
                    tempSever = new Server(this.serviceTimeDistributions[i], null);
                    servers[i] = tempSever;
                }
            }
        }

        private void randomRadioBtn_CheckedChanged(object sender, EventArgs e)
        {
            serverSelection = new ServerSelection(ServerSelection.ServerSelectionTypes.Random);
            //serverSelection = ServerSelection.ServerSelectionTypes.Random;
        }

        private void lowUtilizationRadioBtn_CheckedChanged(object sender, EventArgs e)
        {
            serverSelection = new ServerSelection(ServerSelection.ServerSelectionTypes.LowestUtilization);
        //    serverSelection = ServerSelection.ServerSelectionTypes.LowestUtilization;
        }

        private void rdBtnExp_CheckedChanged(object sender, EventArgs e)
        {
            this.txtExpMean.Enabled = true;
            this.txtMax.Enabled = false;
            this.txtMin.Enabled = false;
            this.dataGridInterarrivalTime.Enabled = false;
        }

        private void rdBtnUniform_CheckedChanged(object sender, EventArgs e)
        {
            this.txtMin.Enabled = true;
            this.txtMax.Enabled = true;
            this.txtExpMean.Enabled = false;
            this.dataGridInterarrivalTime.Enabled = false;
        }

        private List<IDistribution> serviceTimeDistributions;
        private void btnServiceTime_Click(object sender, EventArgs e)
        {
            if (this.numServersTxt.Text == string.Empty)
            {
                MessageBox.Show("Please specify the number of servers.");
            }
            else
            {
                ServiceTimeDistribution serviceTime = new ServiceTimeDistribution(int.Parse(numServersTxt.Text));
                serviceTime.ShowDialog();
                serviceTimeDistributions = serviceTime.ServiceTimeDist;
                this.SelectionCondition();
                this.BtnStartSimulation.Enabled = true;
            }
        }

        private void btnPerformance_Click(object sender, EventArgs e)
        {
            PerformanceMeasures performance = new PerformanceMeasures(ref simManager);
            performance.ShowDialog();
        }
    }
}
